import lunisolar from "lunisolar";
import { Solar, Lunar, HolidayUtil } from "lunar-javascript";

function formatToChineseDateTime(datetime) {
  let solarDate = Solar.fromDate(datetime);
  let lunarDate = Lunar.fromDate(datetime);
  let dateStr = solarDate.toYmdHms();
  let yingliDate = lunarDate.toString();
  // 解析输入字符串为 Date 对象
  const [datePart, timePart] = dateStr.split(" ");
  const [year, month, day] = datePart.split("-");
  const [hour, minute] = timePart.split(":");

  // 构建 Date 对象（注意：月份在 JavaScript 中从 0 开始，需减 1）
  const date = new Date(year, month - 1, day, hour, minute);

  // 提取各时间组件（注意：getMonth() 返回 0-11，需加 1）
  const formattedYear = date.getFullYear();
  const formattedMonth = date.getMonth() + 1;
  const formattedDay = date.getDate();
  const formattedHour = date.getHours();
  const formattedMinute = date.getMinutes();
  let gongYuanDate = `${formattedYear}年${formattedMonth}月${formattedDay}日${formattedHour}点${formattedMinute}分`;
  // 拼接成目标格式
  //处理干支日期
  let gzYear = lunarDate.getYearInGanZhiExact();
  let gzMonth = lunarDate.getMonthInGanZhiExact();
  let gzDay = lunarDate.getDayInGanZhiExact();
  let gzHour = lunarDate.getTimeInGanZhi();

  return {
    yingliDate: yingliDate,
    gongliDate: gongYuanDate,
    ganzhiDate: {
      gzYear,
      gzMonth,
      gzDay,
      gzHour,
    },
    currentJieQi: lunarDate.getPrevJieQi().getName(),
    weekDay: lunarDate.getWeekInChinese(),
    festival: lunarDate.getFestivals(),
    otherFestival: lunarDate.getOtherFestivals(),
    wuHou: lunarDate.getWuHou(),
    hou: lunarDate.getHou(),
    xunShou: lunarDate.getDayXun(),
    xunKong: lunarDate.getDayXunKongExact(),
    zhiXing: lunarDate.getZhiXing(),
    pengZu: {
      pzg: lunarDate.getPengZuGan(),
      pzz: lunarDate.getPengZuZhi(),
    },
  };
}

export { formatToChineseDateTime };
